package org.utils.statemachine {
	public class State 	{
		private var _name:String;
		private var _from:Object;
		private var _enter:Function;
		private var _exit:Function;
		private var _parent:State;
		private var children:Array;
		
		public function State(name:String, from:Object = null, enter:Function = null, exit:Function = null, parent:State = null) { 
			this.name = name;
			if (!from) from = "*";
			this.from = from;
			this.enter = enter;
			this.exit = exit;
			this.children = [];
			if (parent)
			{
				_parent = parent;
				_parent.children.push(this);
			}
		}
		
		public function set parent(parent:State):void {
			_parent = parent;
			_parent.children.push(this);
		}
		
		public function get parent():State {
			return _parent;
		}
		
		public function get root():State {
			var parentState:State = _parent;
			if(parentState)
			{
				while (parentState.parent)
				{
					parentState = parentState.parent;
				}
			}
			return parentState;
		}
		public function get parents():Array {
			var parentList:Array = [];
			var parentState:State = _parent;
			if(parentState) {
				parentList.push(parentState);
				while (parentState.parent) {
					parentState = parentState.parent;
					parentList.push(parentState);
				}
			}
			return parentList;
		}
		
		public function get name():String { return _name; }
		
		public function set name(value:String):void {
			_name = value;
		}
		
		public function get from():Object { return _from; }
		
		public function set from(value:Object):void {
			_from = value;
		}
		
		public function get enter():Function { return _enter; }
		
		public function set enter(value:Function):void {
			_enter = value;
		}
		
		public function get exit():Function { return _exit; }
		
		public function set exit(value:Function):void {
			_exit = value;
		}
		
		public function toString():String {
			return this.name;
		}
	}
}